<script setup lang="ts">
import KnowledgeCard from './knowledgeCard.vue'
import { ref, defineProps } from 'vue'
import type { KnowledgeType, KnowledgeParams, KnowledgeList } from '@/types/consult'

import { getKonwledgeList } from '@/services/consult'

const props = defineProps<{
  type: KnowledgeType
}>()

const loading = ref(false) //加载
const finished = ref(false) //是否完全加载完毕数据

const list = ref<KnowledgeList>([])
const params = ref<KnowledgeParams>({
  type: props.type,
  current: 1,
  pageSize: 10
})
/**
 * 滚到底部
 */
// 假设初始时 current 为 1 或其他代表第一页的值
const onLoad = async () => {
  // 根据当前页码请求数据
  const res = await getKonwledgeList(params.value)

  list.value.push(...res.data.rows)

  // 判断是否还有更多页面
  if (params.value.current >= res.data.pageTotal) {
    finished.value = true
  } else {
    // 加载下一页数据
    params.value.current++
  }
  loading.value = false
}
</script>

<template>
  <div class="knowledge-list">
    <van-list
      v-model:loading="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <knowledge-card v-for="item in list" :key="item.id" :item="item"></knowledge-card>
    </van-list>
  </div>
</template>

<style lang="scss" scoped>
.knowledge-list {
  padding: 0 15px;
}
</style>
